﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lincolnwood.Foundation.Common.Data;

namespace Lincolnwood.Foundation.Scheduling
{
    public class RecurrableEventDefinition : AuditableEntity<Guid>
    {
        private bool _isRecurring;
        private RecurrencePattern _recurrence;
        private Guid _scheduleId;
        private string _name;
        private string _description;
        private DateTime _startTime;
        private DateTime _endTime;

        public RecurrableEventDefinition()
        { }

        public RecurrableEventDefinition(Guid scheduleId, string name, string description, DateTime startTime, DateTime endTime)
        {
            _name = name;
            _description = description;
            _startTime = startTime;
            _endTime = endTime;
            _scheduleId = scheduleId;
        }

        public virtual string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }

        public virtual Guid ScheduleId
        {
            get { return this._scheduleId; }
            private set { this._scheduleId = value; }
        }

        public virtual DateTime StartTime
        {
            get
            {
                return this._startTime;
            }
            set
            {
                this._startTime = value;
            }
        }

        public virtual DateTime EndTime
        {
            get
            {
                return this._endTime;
            }
            set
            {
                this._endTime = value;
            }
        }

        public virtual string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }


        public virtual bool IsRecurring
        {
            get
            {
                return this._isRecurring;
            }
        }

        public virtual RecurrencePattern Recurrence
        {
            get { return this._recurrence; }
            set
            {
                if (value == null)
                    this._isRecurring = false;
                else
                    this._isRecurring = true;
                this._recurrence = value;
            }
        }

        public void ClearRecurrence()
        {
            this.Recurrence = null;
        }

        public void SetDailyRecurrence(DateTime endDate, int dailyRecurrenceInterval)
        {
            DailyRecurrencePattern recurrence =
                new DailyRecurrencePattern(endDate, dailyRecurrenceInterval);
            this.Recurrence = recurrence;
        }

        public void SetWeeklyRecurrence( DateTime endDate, int weeklyRecurrenceInterval)
        {
            WeeklyRecurrencePattern recurrence =
                new WeeklyRecurrencePattern(endDate, weeklyRecurrenceInterval);
            this.Recurrence = recurrence;
        }

        public void SetMonthlyRecurrence(DateTime endDate, int monthlyRecurrenceInterval)
        {
            MonthlyRecurrencePattern recurrence =
                new MonthlyRecurrencePattern(endDate, monthlyRecurrenceInterval);
            this.Recurrence = recurrence;
        }

        public void SetYearlyRecurrence(DateTime endDate, int yearlyRecurrenceInterval)
        {
            YearlyRecurrencePattern recurrence =
                new YearlyRecurrencePattern(endDate, yearlyRecurrenceInterval);
            this.Recurrence = recurrence;
        }

        #region IComparable<CalendarEvent> Members

        public int CompareTo(RecurrableEventDefinition other)
        {
            return this.StartTime.CompareTo(other.StartTime);
        }

        #endregion

        public IEnumerable<IEventInstance> ExpandInstances(DateTime expandBegin, DateTime expandEnd)
        {
            List<IEventInstance> expandedInstances = new List<IEventInstance>();
            if (this.IsRecurring)
                expandedInstances.AddRange(this.Recurrence.Expand(this, expandBegin, expandEnd));
            else
                expandedInstances.Add(new EventInstance(this, this.Name, this.Description, this.StartTime, this.EndTime));
            return expandedInstances;

        }
    }
}
